<template>
  <s-modal
    title="修改"
    :visible="visible"
    width="800px"
    ok-text="保存"
    :confirm-loading="confirmLoading"
    :closable="!confirmLoading"
    @ok="save()"
    @cancel="
      () => {
        if (!confirmLoading) {
          visible = false
        }
      }
    "
  >
    <div style="height: 400px">
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item
            label="流程名称"
            name="flowName"
            :rules="[
              { whitespace: true, required: true, message: '流程名称不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.flowName" allow-clear placeholder="流程名称" />
          </s-form-item>
          <s-form-item
            label="流程类型"
            name="flowType"
            :rules="[{ whitespace: true, required: true, message: '流程类型不能为空' }]"
          >
            <s-select
              v-model="formValue.flowType"
              code="FLOW_TYPE"
              allow-clear
              placeholder="流程类型"
            />
          </s-form-item>
          <s-form-item
            label="流程存储策略"
            name="flowStoragePolicy"
            size="custom"
            tip="用来存储当前流程的各种数据。当选择业务表冗余时，业务表内必须包括所有的冗余字段。当选择新建表时，无需创建表，表会自动创建。"
            :options="{ labelWidth: 8, wrapperWidth: 16, offsetWidth: 0, total: 12 }"
            :rules="[{ whitespace: true, required: true, message: '存储策略不能为空' }]"
          >
            <s-select
              v-model="formValue.flowStoragePolicy"
              code="FLOW_RUN_STORAGE_POLICY"
              allow-clear
              placeholder="流程存储策略"
            />
          </s-form-item>
          <s-form-item
            label="流程存储表"
            name="flowStorageTable"
            :rules="[{ whitespace: true, required: true, message: '存储表不能为空' }]"
          >
            <s-input v-model="formValue.flowStorageTable" allow-clear placeholder="流程存储表" />
          </s-form-item>

          <s-form-item
            label="任务存储策略"
            name="taskStoragePolicy"
            size="custom"
            tip="用来存储当前流程执行中的任务信息。"
            :options="{ labelWidth: 8, wrapperWidth: 16, offsetWidth: 0, total: 12 }"
            :rules="[{ whitespace: true, required: true, message: '存储策略不能为空' }]"
          >
            <s-select
              v-model="formValue.taskStoragePolicy"
              code="TASK_RUN_STORAGE_POLICY"
              allow-clear
              placeholder="任务存储策略"
            />
          </s-form-item>
          <s-form-item
            v-if="formValue.taskStoragePolicy == '2'"
            label="任务存储表"
            name="taskStorageTable"
            :rules="[{ whitespace: true, required: true, message: '存储表不能为空' }]"
          >
            <s-input v-model="formValue.taskStorageTable" allow-clear placeholder="任务存储表" />
          </s-form-item>
          <s-form-item
            label="历史存储策略"
            name="historyStoragePolicy"
            size="custom"
            tip="用来存储当前流程的历史数据。当选择通用表存储时，历史数据会存入skrivet_work_flow_history中。当选择新建表时，无需创建表，表会自动创建。"
            :options="{ labelWidth: 8, wrapperWidth: 16, offsetWidth: 0, total: 12 }"
            :rules="[{ whitespace: true, required: true, message: '历史存储策略不能为空' }]"
          >
            <s-select
              v-model="formValue.historyStoragePolicy"
              code="FLOW_HISTORY_STORAGE_POLICY"
              allow-clear
              placeholder="历史存储策略"
            />
          </s-form-item>
          <s-form-item
            v-if="formValue.historyStoragePolicy == '2'"
            label="历史存储表"
            name="historyStorageTable"
            :rules="[{ whitespace: true, required: true, message: '历史存储策略不能为空' }]"
          >
            <s-input v-model="formValue.historyStorageTable" allow-clear placeholder="历史存储表" />
          </s-form-item>
          <s-form-item label="流程描述" name="flowDescribe" size="big">
            <s-text-area v-model="formValue.flowDescribe" />
          </s-form-item> </s-form
      ></s-scroll>
    </div>
  </s-modal>
</template>

<script>
export default {
  name: 'Update',
  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false
    }
  },
  methods: {
    open(id) {
      this.$loading(true)
      this.$api
        .GET('/workflow/define/selectOneById', { id: id })
        .then((res) => {
          if (!res.data) {
            this.$loading(false)
            this.visible = false
            this.$message.warn('数据不存在')
          } else {
            this.formValue = res.data
            this.$loading(false)
            this.visible = true
          }
        })
        .catch(() => {
          this.$loading(false)
        })
    },
    save() {
      const form = this.$refs.form
      form.validate((values) => {
        this.confirmLoading = true
        values.id = this.formValue.id
        this.$api
          .PUT('/workflow/define/update', values)
          .then((res) => {
            this.confirmLoading = false
            this.visible = false
            this.$emit('success')
            this.$message.info(res.msg)
          })
          .catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
